package suzf.top.kotlinjava.service.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import suzf.top.kotlinjava.common.config.ApiConfig
import suzf.top.kotlinjava.common.config.CommonConstant
import suzf.top.kotlinjava.common.config.RespResult
import suzf.top.kotlinjava.common.entity.User
import suzf.top.kotlinjava.service.service.UserService

/**
 *  用户表 前端控制器
 *========================
 * Created with IntelliJ IDEA
 * Author: Mr.Su
 * Blog: https://suzf.top/
 * Date: 2019-10-10
 * ========================
 */
@RestController
@RequestMapping(value = ApiConfig.API+"/user")
class UserController {

    @Autowired
    lateinit var userService: UserService

    /**
     *  查询全部数据
     */
    @GetMapping(value = "/findAll")
    fun findAll(): RespResult {
        return RespResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS_MSG,true, userService.selectAll())
    }

    /**
     *  查询全部数据的部分字段
     */
    @GetMapping(value = "/findAll1")
    fun findAll1(): RespResult {
        return RespResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS_MSG,true, userService.selectAll1())
    }

    /**
     *  根据ID查询单个数据
     */
    @GetMapping(value = "/findOneById/{id}")
    fun findOneById(@PathVariable id: Int): RespResult {
        return RespResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS_MSG, true,userService.findOneById(id))
    }

    /**
     *  根据ID删除单个数据
     */
    @DeleteMapping(value = "/deleteById/{id}")
    fun deleteById(@PathVariable id: Int): RespResult {
        userService.deleteById(id)
        return RespResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS_MSG,true)
    }

    /**
     *  添加数据
     */
    @PostMapping(value = "/insert")
    fun insertUser(@RequestBody user: User): RespResult {
        userService.insertUser(user)
        return RespResult(CommonConstant.SUCCESS_CODE, CommonConstant.SUCCESS_MSG,true)
    }
}
